package com.situ.ftbserver.api;

import com.github.pagehelper.PageInfo;
import com.situ.ftbserver.model.Course;
import com.situ.ftbserver.model.Reserve;
import com.situ.ftbserver.model.search.CourseSearchBean;
import com.situ.ftbserver.service.CourseService;
import com.situ.ftbserver.service.ReserveService;
import com.situ.ftbserver.util.JsonResult;
import com.situ.ftbserver.util.PaginateInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/v1/course",produces = MediaType.APPLICATION_JSON_VALUE)
public class CourseApi {
    private CourseService courseService;
    private ReserveService reserveService;
    @Autowired
    public void setCourseService(CourseService courseService) {
        this.courseService = courseService;
    }
    @Autowired
    public void setReserveService(ReserveService reserveService) {
        this.reserveService = reserveService;
    }

    @GetMapping
    public ResponseEntity<JsonResult<?>> findAll(@RequestParam(defaultValue ="1")  Integer pageNo,
                                                 @RequestParam(defaultValue ="10") Integer pageSize,
                                                 CourseSearchBean csb){
//        System.out.println(csb);
        PaginateInfo pi = PaginateInfo.of(pageNo, pageSize);
        List<Course> courses = courseService.findAll(pi, csb);
        PageInfo<Course> pageInfo = new PageInfo<>(courses);
        return ResponseEntity.ok(JsonResult.success(pageInfo));
    }
    @GetMapping("/getCourse")
    public ResponseEntity<JsonResult<?>> getCourse(){
        List<Course> courses = courseService.getCourse();
        return ResponseEntity.ok(JsonResult.success(courses));
    }
    @GetMapping("/getPopularCourse")
    public ResponseEntity<JsonResult<?>> getPopularCourse(){
        List<Map<String, Object>> courses = courseService.getPopularCourse();
        return ResponseEntity.ok(JsonResult.success(courses));
    }
    @DeleteMapping
    public ResponseEntity<JsonResult<?>> delete(@RequestBody Integer[] ids){
        int i = reserveService.findByCourseId(ids).size();
        if(i > 0){
            return ResponseEntity.ok(JsonResult.fail("所选课程中含有已被预约课程,不可下架"));
        }else{
            int d = courseService.delete(ids);
            if(d>0){
                return ResponseEntity.ok(JsonResult.success(d));
            }else{
                return ResponseEntity.ok(JsonResult.fail("下架失败"));
            }
        }
    }
    @PostMapping
    public ResponseEntity<JsonResult<?>> save(@RequestBody Course course){
//        System.out.println(course);
        boolean r = courseService.save(course);
        if(r){
            return ResponseEntity.ok(JsonResult.success(course));
        }else{
            return ResponseEntity.ok(JsonResult.fail("添加失败"));
        }
    }
    @PutMapping
    public ResponseEntity<JsonResult<?>> update(@RequestBody Course course){
//        System.out.println(course);
        if(course.getStatus().equals("已取消")){
            List<Reserve> byCourseId = reserveService.findByCourseId(new Integer[]{course.getId()});
            if(byCourseId.size() > 0){
                return ResponseEntity.ok(JsonResult.fail("所选课程中含有已被预约,不可修改"));
            }
        }

        boolean r = courseService.update(course);
        if(r){
            return ResponseEntity.ok(JsonResult.success(course));
        }else{
            return ResponseEntity.ok(JsonResult.fail("修改失败"));
        }
    }
}
